package cn.mst.vrs.service.pay;

import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.mst.vrs.controller.admin.pay.vo.PayPageReqVO;
import cn.mst.vrs.controller.admin.pay.vo.PaySaveReqVO;
import cn.mst.vrs.controller.app.pay.vo.PaySuccessReqVO;
import cn.mst.vrs.dal.dataobject.order.OrderDO;
import cn.mst.vrs.dal.dataobject.pay.PayDO;
import cn.mst.vrs.dal.mysql.order.OrderMapper;
import cn.mst.vrs.dal.mysql.pay.PayMapper;
import cn.mst.vrs.enums.PayStatusEnum;
import cn.mst.vrs.enums.ReservationOrderStatusEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mst.vrs.enums.ErrorCodeConstants.PAY_NOT_EXISTS;

/**
 * 支付单 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PayServiceImpl implements PayService {

    @Resource
    private PayMapper payMapper;

    @Resource
    private OrderMapper orderMapper;

    @Override
    public Long createPay(PaySaveReqVO createReqVO) {
        // 插入
        PayDO pay = BeanUtils.toBean(createReqVO, PayDO.class);
        payMapper.insert(pay);
        // 返回
        return pay.getId();
    }

    @Override
    public void updatePay(PaySaveReqVO updateReqVO) {
        // 校验存在
        validatePayExists(updateReqVO.getId());
        // 更新
        PayDO updateObj = BeanUtils.toBean(updateReqVO, PayDO.class);
        payMapper.updateById(updateObj);
    }

    @Override
    public void deletePay(Long id) {
        // 校验存在
        validatePayExists(id);
        // 删除
        payMapper.deleteById(id);
    }

    private void validatePayExists(Long id) {
        if (payMapper.selectById(id) == null) {
            throw exception(PAY_NOT_EXISTS);
        }
    }

    @Override
    public PayDO getPay(Long id) {
        return payMapper.selectById(id);
    }

    @Override
    public PageResult<PayDO> getPayPage(PayPageReqVO pageReqVO) {
        return payMapper.selectPage(pageReqVO);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean successInvoke(PaySuccessReqVO reqVO) {
        PayDO payDO = validPayExistsById(reqVO.getPayId());
        payDO.setRealAmount(reqVO.getRealAmount());
        payDO.setStatus(PayStatusEnum.SUCCESS.getStatus());
        payMapper.updateById(payDO);

        Long orderId = payDO.getOrderId();
        OrderDO orderDO = orderMapper.selectById(orderId);
        orderDO.setStatus(ReservationOrderStatusEnum.WAIT_USED.getStatus());
        orderMapper.updateById(orderDO);

        return true;
    }

    private PayDO validPayExistsById(Long id) {
        PayDO payDO = payMapper.selectById(id);
        if (ObjUtil.isNull(payDO)) {
            throw exception(PAY_NOT_EXISTS);
        }
        return payDO;
    }

}